package com.wanxi.dao.Impl;

import com.wanxi.dao.TeacherDao;
import com.wanxi.model.Teacher;
import com.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TeacherDaoImpl implements TeacherDao {
    @Override
    public List<Teacher> findAllTeacher() {
        List<Teacher> list =new ArrayList<>();
        String sql="select * from teacher";
        ResultSet query = JDBC.query(sql);
        try{
            while (query.next()){
                Teacher teacher= Teacher.builder()
                        .id(query.getInt("id"))
                        .teacherName(query.getString("teacherName"))
                        .level(query.getString("level"))
                        .imageLocation(query.getString("imageLocation"))
                        .enable(query.getInt("enable"))
                        .createTime(query.getTimestamp("createTime"))
                        .updateTime(query.getTimestamp("updateTime"))
                        .remark(query.getString("remark"))
                        .build();
                list.add(teacher);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public Teacher findTeacherById(Teacher teacher) {
        String sql="select * from teacher where id="+teacher.getId();
        ResultSet query = JDBC.query(sql);
        try {
            while (query.next()){
                return Teacher.builder()
                        .id(query.getInt("id"))
                        .teacherName(query.getString("teacherName"))
                        .level(query.getString("level"))
                        .imageLocation(query.getString("imageLocation"))
                        .enable(query.getInt("enable"))
                        .createTime(query.getTimestamp("createTime"))
                        .updateTime(query.getTimestamp("updateTime"))
                        .remark(query.getString("remark"))
                        .build();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return null;
    }

    @Override
    public int updateEnable(Teacher teacher) {
        String sql="update teacher set enable="+teacher.getEnable()+",updateTime=(select now()) where id="+teacher.getId();
        return JDBC.update(sql);
    }

    @Override
    public int addTeacher(Teacher teacher) {
        String sql="insert into teacher(teacherName,level,imageLocation,enable,createTime,updateTime,remark) "
                +"values('"+teacher.getTeacherName()
                +"','"+teacher.getLevel()
                +"','"+teacher.getImageLocation()
                +"',"+teacher.getEnable()
                +",(select now()),(select now()),'"+teacher.getRemark()
                +"')";
        return JDBC.update(sql);
    }

    @Override
    public int updateTeacher(Teacher teacher) {
        String sql="update teacher set teacherName='"+teacher.getTeacherName()
                +"',level='"+teacher.getLevel()
                +"',imageLocation='"+teacher.getImageLocation()
                +"',enable="+teacher.getEnable()
                +",remark='"+teacher.getRemark()
                +"',updateTime=(select now()) where id="+teacher.getId();
        return JDBC.update(sql);
    }

    @Override
    public int deleteTeacher(Teacher teacher) {
        String sql="delete from teacher where id="+teacher.getId();
        return JDBC.update(sql);
    }
}
